package services;

import javax.jws.WebService;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.jboss.ws.annotation.EndpointConfig;

import data.Db;

import edu.ipfw.fitness.EquipmentCategoryType;
import edu.ipfw.fitness.EquipmentIdListType;
import edu.ipfw.fitness.EquipmentType;
import edu.ipfw.fitness.EquipmentUsageListType;
import edu.ipfw.services.equipment.EquipmentNotFoundFault;
import edu.ipfw.services.equipment.EquipmentService;
import edu.ipfw.services.equipment.EquipmentNotFound;

import java.math.BigDecimal;
import java.util.GregorianCalendar;
import java.util.Hashtable;

@WebService(serviceName = "Equipment",endpointInterface = "edu.ipfw.services.equipment.EquipmentService",targetNamespace = "http://ipfw.edu/services/equipment")
@EndpointConfig(configName="Standard WSSecurity Endpoint")
public class EquipmentServiceImpl implements EquipmentService {

	// should really place into db....
	private Hashtable<String, EquipmentType> equipmentStore = new Hashtable<String, EquipmentType>();

	// Create new database object and open the connection
	Db db = new Db();

	// Creates an Equipment in persistent storage with the given data. Assigns
	// all
	// numeric fields to 0. Sets lastMaintenanceDate to current system date.
	// Creates
	// empty usageHistory. Returns true if Equipment created/stored
	// successfully;
	// false otherwise.
	
	// Retrieves Equipment with given id from persistent storage.
	// Generates EquipmentNotFound fault if Equipment does not exist.
	public EquipmentType getEquipment(String id) throws EquipmentNotFoundFault {

		EquipmentType equipment = null;
		try {
			equipment = db.getEquipment(id);
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (equipment.getId() != null) {
			return equipment;
		} else {
			throwEquipmentNotFoundException();

			return null;
		}

	}

	/*
	 * 
	 * Retrieves all occurrences of Equipment of given category, excluding
	 * usageHistory, from persistent storage. If category is not present,
	 * retrieves all occurrences of Equipment.
	 */
	public EquipmentIdListType getEquipmentList(EquipmentCategoryType arg0) {

		return db.getEquipmentList(arg0);

	}

	/*
	 * Returns usage list of Equipment Ids for Equipment due for maintenance.An
	 * Equipment is due for maintenance if either three months have passedsince
	 * lastMaintenanceDate or currentMileage is greater than or equal to 1000.
	 */
	public EquipmentIdListType getMaintenanceDue() {

		return db.getMaintenanceDue();
	}

	public EquipmentUsageListType getUsage(String arg0)
			throws EquipmentNotFoundFault {

		return db.getUsage(arg0);

	}

	public EquipmentUsageListType getUsageForDataRange(
			XMLGregorianCalendar arg0, XMLGregorianCalendar arg1, String arg2)
			throws EquipmentNotFoundFault {

		return db.getUsageForDateRange(arg0, arg1, arg2);

	}


	private void throwEquipmentNotFoundException()
			throws EquipmentNotFoundFault {
		EquipmentNotFound error = new EquipmentNotFound();
		error.setEquipmentNotFound("This piece of Equipment was not found.");
		throw new EquipmentNotFoundFault("EquipmentNotFound", error);
	}

}
